package com.ws.base.db.support;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ws.base.common.util.WSUtil;
import com.ws.base.db.base.DBBaseOperate;
import com.ws.base.db.common.SqlTemplateHandle;
import com.ws.base.db.common.order.Order;
import com.ws.base.db.common.page.Page;

/**
 * DBSupport Processing Stations sqlString Processing <[, ]> station operator
 * 
 * @author wangshi
 */
public class DBStationsSupport {
    private DBBaseOperate dbBaseOperate;

    public int add(String sql, Map<String, Object> condition) {
        return dbBaseOperate.add(
                SqlTemplateHandle.getInstance().execute(sql, condition),
                condition);
    }

    public int[] addBatch(String sqlId, Map<String, Object>[] conditions) {
        return dbBaseOperate.addBatch(sqlId, conditions);
    }

    public int delete(String sql, Map<String, Object> condition) {
        return dbBaseOperate.delete(
                SqlTemplateHandle.getInstance().execute(sql, condition),
                condition);
    }

    public int update(String sql, Map<String, Object> condition) {
        return dbBaseOperate.update(
                SqlTemplateHandle.getInstance().execute(sql, condition),
                condition);
    }

    public <T> List<T> queryListForObject(String sql,
            Map<String, Object> condition, Page page, Class<T> clazz) {
        return dbBaseOperate.queryListForObject(SqlTemplateHandle.getInstance()
                .execute(sql, condition), condition, page, clazz);
    }

    public List<Map<String, Object>> query(String sql,
            Map<String, Object> condition, Page page, Order order) {
        if (null != order && !WSUtil.isEmpty(order.getFieldName())) {
            if (null == condition) {
                condition = new HashMap<String, Object>();
            }

            condition.put("orderStr", "ORDER BY " + order.getFieldName()
                    + (order.isAsc() ? " ASC" : " DESC"));
        }

        return dbBaseOperate.query(
                SqlTemplateHandle.getInstance().execute(sql, condition),
                condition, page);
    }

    public int count(String sql, Map<String, Object> condition) {
        return dbBaseOperate.count(
                SqlTemplateHandle.getInstance().execute(sql, condition),
                condition);
    }

    public DBBaseOperate getDbBaseOperate() {
        return dbBaseOperate;
    }

    public void setDbBaseOperate(DBBaseOperate dbBaseOperate) {
        this.dbBaseOperate = dbBaseOperate;
    }

}
